;;; RLlib copyright 2010 Adam White (theroguemonkey@gmail.com)
;;;; Licensed under GNU General Public Licence v2 or later - refer COPYING

(in-package #:rllib)


;;;============================================================================
;;; Audio
;;;============================================================================
(cffi:defcstruct sdl-audiospec
  (freq :int)
  (format :uint16)
  (channels :uint8)
  (silence :uint8)
  (samples :uint16)
  (padding :uint16)
  (size :uint32)
  (callback :pointer) ; void (*callback)(void *userdata, Uint8 *stream, int len);
	
  (userdata :pointer))

(defconstant +audio-u8+ #x0008)
(defconstant +audio-s8+ #x8008)
(defconstant +audio-u16lsb+ #x0010)
(defconstant +audio-s16lsb+ #x8010)
(defconstant +audio-u16msb+ #x1010)
(defconstant +audio-s16msb+ #x9010)
(defconstant +audio-u16+ #x0010)
(defconstant +audio-s16+ #x89010)


(cffi:defcstruct sdl-audiocvt
  (needed :int)
  (src-format :uint16)
  (dst-format :uint16)
  (rate-incr :double)
  (buf :pointer)
  (len :int)
  (len-cvt :int)
  (len-mult :int)
  (filters :pointer)  ; void (*filters[10])(struct SDL_AudioCVT *cvt, Uint16 format);	
  (filter-index :int))


(cffi:defcfun ("SDL_AudioDriverName" sdl-audio-driver-name) :string
  (namebuf :string)
  (maxlen :int))

(cffi:defcfun ("SDL_OpenAudio" sdl-open-audio) :int
  (desired sdl-audiospec)
  (obtained sdl-audiospec))

(cffi:defcenum sdl-audiostatus
  :audio-stopped
  :audio-playing
  :audio-paused)

(cffi:defcfun ("SDL_GetAudioStatus" sdl-audio-status) :void)

(cffi:defcfun ("SDL_PauseAudio" sdl-pause-audio) :void
  (pause-on :int))

(cffi:defcfun ("SDL_LoadWAV_RW" %sdl-load-wav-rw) sdl-audiospec
  (src :pointer)
  (freesrc :int)
  (spec sdl-audiospec)
  (audio-buf :pointer)
  (audio-len :pointer))

(defun sdl-load-wav (file spec audio-buf audio-len)
  (%sdl-load-wav-rw (%sdl-rw-from-file file "rb") 1 spec audio-buf audio-len))

(cffi:defcfun ("SDL_FreeWAV" sdl-free-wav) :void
  (audio-buf :pointer))

(cffi:defcfun ("SDL_BuildAudioCVT" sdl-build-audio-cvt) :int
  (cvt sdl-audiocvt)
  (src-format :uint16)
  (src-channels :uint8)
  (src-rate :int)
  (dst-format :uint16)
  (dst-channels :uint8)
  (dst-rate :int))

(cffi:defcfun ("SDL_ConvertAudio" sdl-convert-audio) :int
  (cvt sdl-audiocvt))

(defconstant +sdl-mix-max-volume+ 128)

(cffi:defcfun ("SDL_MixAudio" sdl-mix-audio) :void
  (dst :pointer)
  (src :pointer)
  (len :uint32)
  (volume :int))

(cffi:defcfun ("SDL_LockAudio" sdl-lock-audio) :void)

(cffi:defcfun ("SDL_UnlockAudio" sdl-unlock-audio) :void)

(cffi:defcfun ("SDL_CloseAudio" sdl-close-audio) :void)

(defmacro with-sdl-audio-lock (&body body)
  `(unwind-protect
	(progn
	  (sdl-lock-audio)
	  ,@body)
     (sdl-unlock-audio)))



;;;============================================================================
;;; Mixer
;;;============================================================================


